﻿package app.view.components.guiDima 
{
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.events.UIEvent;
	import com.snzflash.components.list.ListItemRenderer;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	/**
	 * Для представления видео (по факту картинка +текст)
	 * @author www
	 */
	public class VideoItem extends ListItemRenderer
	{
		public static const WIDTH:Number = 200;
		public static const HEIGHT:Number = 280;
		
		public static const IMAGE_WIDTH:Number = WIDTH;
		public static const IMAGE_HEIGHT:Number = 150;
		public static const GAP_TEXT:Number = 10;
		private var _image:Image;
		private var _border:Shape;
		private var _mask:Shape;
		private var _field:TextField;
		
		public function VideoItem() 
		{
			super();
			
			
		}
		
		override protected function init():void
		{
			super.init();
			
			graphics.beginFill(0xff0000,0);
			graphics.drawRect(0, 0, WIDTH, HEIGHT);
			graphics.endFill();
			
			_image = new Image(IMAGE_WIDTH, IMAGE_HEIGHT, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image);
			
			_border = new Shape();
			_border.graphics.lineStyle(1, 0x000000, 1, true, LineScaleMode.NONE);
			_border.graphics.drawRoundRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,10)
			addChild(_border);
			
			_mask = new Shape();
			_border.graphics.lineStyle(1, 0x000000, 1, true, LineScaleMode.NONE);
			_border.graphics.drawRoundRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,10)
			addChild(_mask);
			_image.mask = _mask;
			
			var format:TextFormat = new TextFormat();
			format.size = 20;
			format.kerning = true;
			format.align = TextFormatAlign.LEFT;
			//format.font='Arial'
			
			_field = new TextField();
			_field.x = 0;
			_field.y = IMAGE_HEIGHT+GAP_TEXT;
			_field.width = WIDTH;
			_field.height = HEIGHT-IMAGE_HEIGHT-GAP_TEXT;
			_field.wordWrap = true;
			_field.multiline=true
			_field.mouseEnabled = false;
			_field.defaultTextFormat = format;
			//_field.embedFonts = true;
			_field.antiAliasType = AntiAliasType.ADVANCED;
			//_field.htmlText = '999999999999999999';
			_field.autoSize = TextFieldAutoSize.LEFT;
			addChild(_field);
			
		};
		
		private function setEvents():void
		{
			doubleClickEnabled = true;
			//addEventListener(MouseEvent.ROLL_OVER, onItemOver);
			//addEventListener(MouseEvent.ROLL_OUT, onItemOut);
			addEventListener(MouseEvent.MOUSE_DOWN, onClick);
		}
		
		private function removeEvents():void
		{
			//removeEventListener(MouseEvent.ROLL_OVER, onItemOver);
			//removeEventListener(MouseEvent.ROLL_OUT, onItemOut);
			removeEventListener(MouseEvent.MOUSE_DOWN, onClick);
		}
		
		override public function destroy():void 
		{
			super.destroy();
			
			removeEvents();
			
			removeChild(_field);
			
			_image.destroy();
			removeChild(_image);
			
			removeChild(_mask);
			removeChild(_border);
			
			
		};
		
		override public function get data():Object { return super.data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			//trace('data', data);
			
			_image.loadImage(_data.image);
			_field.htmlText = _data.text_fio;
			
			selected = false;
		};
		
		override public function get selected():Boolean { return _selected; };
		
		override public function set selected(value:Boolean):void 
		{
			_selected = value;
			
			/*if (_selected==true) 
			{
				removeEvents();
				buttonMode = useHandCursor = false;
			}else
			{
				setEvents();
				buttonMode = useHandCursor = true;
			}*/
			
			setEvents();
			buttonMode = useHandCursor = true;
		};
		
		private function onClick(e:MouseEvent):void 
		{
			//trace('click popup');
			
			var ev:UIEvent = new UIEvent(UIEvent.CLICK_INTERVIEW_WINNER);
			ev.data = data;
			dispatchEvent(ev);
			
		}
		
		
	}

}